package com.wude.tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
import com.mc.developmentkit.bean.WFTPayInfo;
import com.mc.developmentkit.bean.WXPayInfo;
import com.mc.developmentkit.bean.ZFBPayInfo;
import com.mc.developmentkit.callback.AliPayListener;
import com.mc.developmentkit.config.MCConstant;
import com.mc.developmentkit.https.HttpUtils;
import com.mc.developmentkit.json.Json;
import com.mc.developmentkit.utils.ToastUtil;
import com.switfpass.pay.MainApplication;
import com.switfpass.pay.activity.PayPlugin;
import com.switfpass.pay.bean.RequestMsg;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.wude.bean.GetPaidInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/21 0021.
 */
@SuppressLint("HandlerLeak")
public class ThirdPartyPay {
    private static ThirdPartyPay instance;
    private ZFBPayInfo zfbPayInfo;
    private Map<String, Object> map;
    private String json;
    private String json1;
    private String msg1;
    private String appid;
    private Activity activity;
    Handler vHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("威富通和微信json", msg.obj.toString());

                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        String msg2 = jsonObject.getString("msg");
                        if (status == 1) {
                            String wxtype = jsonObject.getString("wxtype");
                            if (wxtype.equals("wft")) {
                                RequestMsg requestMsg = new RequestMsg();
                                requestMsg.setTokenId(jsonObject.getString("token_id"));
                                requestMsg.setAppId(ThirdPartyPay.this.appid);
                                requestMsg.setTradeType(MainApplication.WX_APP_TYPE);
                                PayPlugin.unifiedAppPay(ThirdPartyPay.this.activity, requestMsg);
                            } else {
                                IWXAPI api = WXAPIFactory.createWXAPI(ThirdPartyPay.this.activity, (String) null);
                                api.registerApp(jsonObject.getString("appid"));
                                PayReq request = new PayReq();
                                request.appId = jsonObject.getString("appid");
                                request.partnerId = jsonObject.getString("partnerid");
                                request.prepayId = jsonObject.getString("prepayid");
                                request.nonceStr = jsonObject.getString("noncestr");
                                request.timeStamp = jsonObject.getString("timestamp");
                                request.packageValue = jsonObject.getString("package");
                                request.sign = jsonObject.getString("sign");
                                api.sendReq(request);
                            }
                        } else {
                            ToastUtil.showToast(msg2);
                        }
                    } catch (JSONException var8) {
                        var8.printStackTrace();
                        Log.e("解析威富通和微信异常", var8.toString());
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
            }

        }
    };
    Handler tHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    WFTPayInfo wftPayInfo = Json.DNSWFTPay(msg.obj.toString());
                    if (wftPayInfo != null) {
                        RequestMsg requestMsg = new RequestMsg();
                        requestMsg.setTokenId(wftPayInfo.tokenId);
                        requestMsg.setAppId(MCConstant.getWxAppId());
                        requestMsg.setTradeType(MainApplication.WX_APP_TYPE);
                        PayPlugin.unifiedAppPay(ThirdPartyPay.this.activity, requestMsg);
                    } else {
                        ToastUtil.showToast("微付通订单信息为空");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
            }

        }
    };
    Handler xHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    WXPayInfo wxPayInfo = Json.DNSWXPay(msg.obj.toString());
                    if (wxPayInfo == null) {
                        return;
                    }

                    IWXAPI api = WXAPIFactory.createWXAPI(ThirdPartyPay.this.activity, (String) null);
                    api.registerApp(wxPayInfo.appid);
                    PayReq request = new PayReq();
                    request.appId = wxPayInfo.appid;
                    request.partnerId = wxPayInfo.partnerid;
                    request.prepayId = wxPayInfo.prepayid;
                    request.nonceStr = wxPayInfo.noncestr;
                    request.timeStamp = wxPayInfo.timestamp;
                    request.packageValue = wxPayInfo.packagvalues;
                    request.sign = wxPayInfo.sign;
                    api.sendReq(request);
                    break;
                case 2:
                    ToastUtil.showToast("网络异常！");
            }

        }
    };
    Handler zHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ThirdPartyPay.this.zfbPayInfo = Json.DNSZFBPay(ThirdPartyPay.this.activity, msg.obj.toString());
                    ThirdPartyPay.this.map = new HashMap();
                    Log.e("zfbPayInfo.orderInfo", ThirdPartyPay.this.zfbPayInfo.orderInfo + "---");
                    Runnable payRunnable = new Runnable() {
                        public void run() {
                            PayTask alipay = new PayTask(ThirdPartyPay.this.activity);
                            String result = alipay.pay(ThirdPartyPay.this.zfbPayInfo.orderInfo, true);
                            Message msg = new Message();
                            msg.what = 1;
                            msg.obj = result;
                            ThirdPartyPay.this.mHandler.sendMessage(msg);
                        }
                    };
                    Thread payThread = new Thread(payRunnable);
                    payThread.start();
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
            }

        }
    };
    private AliPayListener aliPayListener;
    @SuppressLint({"HandlerLeak"})
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    com.switfpass.pay.activity.PayResult payResult = new com.switfpass.pay.activity.PayResult((String) msg.obj);
                    String resultInfo = payResult.getResult();
                    String resultStatus = payResult.getResultStatus();
                    Log.e("支付返回码：", resultStatus + "---");
                    if (TextUtils.equals(resultStatus, "9000")) {
                        ThirdPartyPay.this.msg1 = "支付成功";
                        ThirdPartyPay.this.json1 = "{\"msg\":\"" + ThirdPartyPay.this.msg1 + "\",\"code\":" + resultStatus + "}";
                    } else if (TextUtils.equals(resultStatus, "8000")) {
                        ThirdPartyPay.this.msg1 = "等待支付结果";
                        ThirdPartyPay.this.json1 = "{\"msg\":\"" + ThirdPartyPay.this.msg1 + "\",\"code\":\"" + resultStatus + "\"}";
                    } else if (TextUtils.equals(resultStatus, "4001")) {
                        ThirdPartyPay.this.msg1 = "支付成功";
                        ThirdPartyPay.this.json1 = "{\"msg\":\"" + ThirdPartyPay.this.msg1 + "\",\"code\":\"" + resultStatus + "\"}";
                    } else {
                        ThirdPartyPay.this.msg1 = "支付失败";
                        ThirdPartyPay.this.json1 = "{\"msg\":\"" + ThirdPartyPay.this.msg1 + "\",\"code\":" + resultStatus + "}";
                    }

                    if (ThirdPartyPay.this.aliPayListener == null) {
                        ToastUtil.showToast(ThirdPartyPay.this.msg1);
                    } else {
                        ThirdPartyPay.this.aliPayListener.zfbPayResult(ThirdPartyPay.this.json1);
                    }
                default:
            }
        }
    };

    private ThirdPartyPay() {
    }

    public static ThirdPartyPay getInstance() {
        if (null == instance) {
            instance = new ThirdPartyPay();
        }

        return instance;
    }

    public void wftPay(Activity activity, String url, GetPaidInfo getPaidInfo) {
        this.activity = activity;
        HttpUtils.POST(this.tHandler, url, this.goodsInfo(getPaidInfo), false);
    }

    public void wxPay(Activity activity, String url, GetPaidInfo getPaidInfo) {
        this.activity = activity;
        HttpUtils.POST(this.xHandler, url, this.goodsInfo(getPaidInfo), false);
    }

    public void wx2wftPay(Activity activity, String url, GetPaidInfo getPaidInfo, String id) {
        this.activity = activity;
        this.appid = id;
        HttpUtils.POST(this.vHandler, url, this.goodsInfo(getPaidInfo), false);
    }

    public void aliPay(Activity activity, String url, GetPaidInfo getPaidInfo, AliPayListener aliPayListener) {
        this.aliPayListener = aliPayListener;
        this.activity = activity;
        HttpUtils.POST(this.zHandler, url, this.goodsInfo(getPaidInfo), false);
    }

    public HashMap<String, String> goodsInfo(GetPaidInfo getPaidInfo) {
        HashMap map = new HashMap();

        try {
            if (getPaidInfo.good_name != null && !getPaidInfo.good_name.isEmpty()) {
                map.put("good_name", getPaidInfo.good_name);
            }

            if (getPaidInfo.good_id != null && !getPaidInfo.good_id.isEmpty()) {
                map.put("good_id", getPaidInfo.good_id);
            }

            if (getPaidInfo.good_price != null && !getPaidInfo.good_price.isEmpty()) {
                map.put("price", getPaidInfo.good_price);
            }

            if (getPaidInfo.userId != null && !getPaidInfo.userId.isEmpty()) {
                map.put("user_id", getPaidInfo.userId);
            }

            if (getPaidInfo.user_name != null && !getPaidInfo.user_name.isEmpty()) {
                map.put("user_name", getPaidInfo.user_name);
            }

            if (getPaidInfo.user_sign != null && !getPaidInfo.user_sign.isEmpty()) {
                map.put("token", getPaidInfo.user_sign);
            }

            if (getPaidInfo.payType != null && !getPaidInfo.payType.isEmpty()) {
                map.put("pay_way", getPaidInfo.payType);
            }

            if (getPaidInfo.body != null && !getPaidInfo.body.isEmpty()) {
                map.put("body", getPaidInfo.body);
            }

            if (getPaidInfo.code != null && !getPaidInfo.code.isEmpty()) {
                map.put("code", getPaidInfo.code);
            }

            if (getPaidInfo.good_title != null && !getPaidInfo.good_title.isEmpty()) {
                map.put("title", getPaidInfo.good_title);
            }

            if (getPaidInfo.packageId != null && !getPaidInfo.packageId.isEmpty()) {
                map.put("package_id", getPaidInfo.packageId);
            }

            if (getPaidInfo.extend != null && !getPaidInfo.extend.isEmpty()) {
                map.put("extend", getPaidInfo.extend);
            }

            if (getPaidInfo.promote_id != null && !getPaidInfo.promote_id.isEmpty()) {
                map.put("promote_id", getPaidInfo.promote_id);
            }

            if (getPaidInfo.pay_amount != null && !getPaidInfo.pay_amount.isEmpty()) {
                map.put("pay_amount", getPaidInfo.pay_amount);
            }

            if (getPaidInfo.account != null && !getPaidInfo.account.isEmpty()) {
                map.put("account", getPaidInfo.account);
            }

            if (getPaidInfo.real_pay_amount != null && !getPaidInfo.real_pay_amount.isEmpty()) {
                map.put("real_pay_amount", getPaidInfo.real_pay_amount);
            }

            JSONObject jsonObject = new JSONObject();
            if (getPaidInfo.good_type == 1) {
                jsonObject.put("type", "1");
            }

            if (getPaidInfo.good_type == 2) {
                jsonObject.put("type", "2");
            }

            if (getPaidInfo.good_type == 3) {
                jsonObject.put("type", "3");
            }

            if (getPaidInfo.game_id != null && !getPaidInfo.game_id.isEmpty()) {
                jsonObject.put("game_id", getPaidInfo.game_id);
            }

            if (getPaidInfo.phone != null && !getPaidInfo.phone.isEmpty()) {
                jsonObject.put("phone", getPaidInfo.phone);
            }

            map.put("good_info", jsonObject.toString());
        } catch (Exception var4) {
            var4.printStackTrace();
            Log.e("获取订单信息异常异常", var4.toString());
        }

        Log.e("商品请求信息", map.toString());
        return map;
    }
}
